/* Copyright 2014 Mozilla Foundation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

const PDF_PATH = "../web/compressed.tracemonkey-pldi-09.pdf";
const DEFAULT_SCALE_VALUE = 1.1;

pdfjsLib.GlobalWorkerOptions.workerSrc =
  "../build/pdf.worker.mjs";


async function pageLoaded() {
  // 创建 PDF 查看器实例
  const container = document.getElementById('viewerContainer');
  const viewer = document.getElementById('viewer');
  const eventBus = new pdfjsViewer.EventBus();
  const pdfViewer = new pdfjsViewer.PDFViewer({
    container: container,
    viewer: viewer,
    eventBus: eventBus,

  });
  eventBus.on("pagesinit", function () {
    // We can use pdfViewer now, e.g. let's change default scale.
    pdfViewer.currentScaleValue = DEFAULT_SCALE_VALUE;
  });
  pdfjsLib.getDocument({ url: PDF_PATH }).promise.then((pdfDocument) => {
      // 将 PDF 文档关联到查看器
      pdfViewer.setDocument(pdfDocument);
  }).catch((error) => {
      console.error('Error loading PDF:', error);
  });
}

document.addEventListener("DOMContentLoaded", function () {
  if (typeof pdfjsLib === "undefined") {
    // eslint-disable-next-line no-alert
    alert("Please build the pdfjs-dist library using\n  `gulp dist-install`");
    return;
  }
  pageLoaded();
});
